package dao.impl;

import dao.IMyOrderDao;
import jdbc_util.FirstApp;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import pojo.MyOrder;

import java.sql.SQLException;
import java.util.List;

/**
 * @author Mazai_
 * @version 1.0
 * @date 2021/5/26 20:25
 */
public class MyOrderDaoImpl implements IMyOrderDao {
    @Override
    public void addMyOrder(MyOrder myOrder) {
        QueryRunner queryRunner = new QueryRunner();
        try {
            String sql = "insert into myorder(userid,cost,orderstaus,paywayid) values (?,?,?,?)";
            queryRunner.update(FirstApp.getConn(), sql, myOrder.getUser().getId(), myOrder.getCost(), myOrder.getOrderStatus().getOrderStatusid(), myOrder.getPayWay().getPaywayid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateMyOrder(MyOrder myOrder) {
        QueryRunner queryRunner = new QueryRunner();
        try {
            String sql = "update myorder set userid = ?cost = ?,orderstaus = ?,paywayid = ?";
            queryRunner.update(FirstApp.getConn(), sql, myOrder.getUser().getId(), myOrder.getCost(), myOrder.getOrderStatus().getOrderStatusid(), myOrder.getPayWay().getPaywayid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteMyOrder(int id) {
        QueryRunner queryRunner = new QueryRunner();
        try {
            String sql = "delete from myorder where id = ?";
            queryRunner.update(FirstApp.getConn(), sql, id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<MyOrder> findAll() {
        QueryRunner queryRunner = new QueryRunner();
        List<MyOrder> myOrderList = null;
        try {
            String sql = "select * from myorder";
            myOrderList = queryRunner.query(FirstApp.getConn(), sql, new BeanListHandler<>(MyOrder.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return myOrderList;
    }

    @Override
    public List<MyOrder> findByUserNameOrId(int userid) {
        QueryRunner queryRunner = new QueryRunner();
        List<MyOrder> myOrderList = null;
        try {
            String sql = "select * from myorder where userid = ?";
            myOrderList = queryRunner.query(FirstApp.getConn(), sql, new BeanListHandler<>(MyOrder.class), userid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return myOrderList;
    }

    @Override
    public MyOrder findById(int id) {
        QueryRunner queryRunner = new QueryRunner();
        MyOrder myOrder = new MyOrder();
        try {
            String sql = "select  * from myorder where myorderid = ?";
            myOrder = queryRunner.query(FirstApp.getConn(), sql, new BeanHandler<>(MyOrder.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return myOrder;
    }
}
